/**
 * @author: Hai Do Minh
 * 
 * @Note:
 * Currently, I'm considering to implement owner history stack instead of JQM 
 * in oder to manage pages history easily and optimize JQM caching DOM and other data.
 * 
 * Idealy, diable page changing history of JQM and uses a stack to manage history.
 * We can get current active page, clear stack, pop-to home page easily,
 * and other advantages as advoiding the missing previous page bug of JQM.
 * 
 * But, how to disable brower to cache the history?
 */ 

function PageChangeController() { }

PageChangeController.prototype = {
    prePageId: '#',
    NO_CHANGE: 'NO-CHANGE',
    EXIT_APP: 'EXIT-APP',
    PRE_PAGE: '',
    
    changeToPage: function(pageId, params, prePageId, pageShowOptions) {
        var curPageId = (typeof prePageId != 'undefined') ? prePageId : $.mobile.activePage.attr("id");
        if (curPageId == pageId) {
            snapper.close();
            return false;
        } else if (PageChangeController.prototype.checkInternetConnection(pageId)){
            Util.prototype.showSpinner();
            if (typeof params != 'undefined' && params != null) {
                PageChangeController.prototype.assignParams(pageId, params);
            }
            PageChangeController.prototype.prePageId = curPageId;
            var toPage = '#' + pageId;
            console.log("changeToPage: " + toPage);
            if (typeof pageShowOptions != 'undefined') {
                $.mobile.changePage($(toPage), pageShowOptions);
            } else {
                $.mobile.changePage($(toPage));
            }
            return true;
        }
    },
    
    goBack: function(prePageId) {
        if (!Util.prototype.isSnapperClosed()) {
            snapper.close();
            return false;
        }
        Util.prototype.showSpinner();
        var curPageId = (typeof prePageId != 'undefined') ? prePageId : $.mobile.activePage.attr("id");        
        var toPage = PageChangeController.prototype.getBackTargetPageId(curPageId);
        console.log("goBack: " + toPage);
        switch(toPage) {
            case PageChangeController.prototype.NO_CHANGE:
                Util.prototype.hideSpinner();
                return false;
            case PageChangeController.prototype.EXIT_APP:
                Util.prototype.hideSpinner();                
                navigator.notification.confirm("アプリケーションを終了しますか？", function(buttonIndex){
                    if (buttonIndex == 1) {
                        navigator.app.exitApp();
                    }                    
                }, "終了確認", ["終了","キャンセル"]);
                return false;
            case PageChangeController.prototype.PRE_PAGE:
                $.mobile.back();
                break;
            default:
                $.mobile.changePage($(toPage));
                break;
        }
        return true;
    },
    
    freeMemory: function(pPageId) {
        var pageId = (typeof pPageId != 'undefined') ? pPageId : PageChangeController.prototype.prePageId;
        if (pageId == '#') {
            return false;
        }
        // clear parameters if possible
        var isPageHome = $.mobile.activePage.attr("id");
        isPageHome = (isPageHome == 'pageHome' || isPageHome == 'pageMonthArchives' || 
                            isPageHome == 'pageGenreArchives' || isPageHome == 'pageKeywordArchives' || 
                            isPageHome == 'pagePrefArchives' || isPageHome == 'pageSetting');                        
        switch(pageId) {
            case 'pageHome':
                IndexController.prototype.freeMemory();
                return;
            case 'pageJapanMap':
                JapanMapController.prototype.freeMemory(true, isPageHome);
                return;
            case 'pageLocalMap':                
                JapanMapController.prototype.freeMemory(false, isPageHome);
                return;
            case 'pageGMap':
                GoogleMapController.prototype.freeMemory(isPageHome);
                return;
            case 'pageMonthArchives':
                MonthController.prototype.freeMemory();
                return;
            case 'pageGenreArchives':
                GenreController.prototype.freeMemory();
                return;
            case 'pageKeywordArchives':
                KeywordController.prototype.freeMemory();
                return;
            case 'pagePrefArchives':
                PrefController.prototype.freeMemory();
                return;
            case 'pageCityArchives':
                CityController.prototype.freeMemory(isPageHome);
                return;
            case 'pageBookmarkList':
                BookmarkListController.prototype.freeMemory(true, isPageHome);
                return; 
            case 'pageBookmarkListSearch':                
                BookmarkListController.prototype.freeMemory(false, isPageHome);
                return;
            case 'pageItemSearch':
                SearchController.prototype.freeMemory(true, isPageHome);
                return;
            case 'pageItemSearchResult':
                SearchController.prototype.freeMemory(false, isPageHome);
                return;
            case 'pageBookmarkRegister':
                BookmarkRegisterController.prototype.freeMemory(isPageHome);
                return; 
            case 'pageItemList':
                DetailProductController.prototype.freeMemory(isPageHome);
                return; 
            case 'pageECsite':
                ECSiteController.prototype.freeMemory(isPageHome);
                return;     
            default:
                return;
        }    
    },
    
    assignParams: function(pageId, params) {
        switch(pageId) {
            case 'pageLocalMap':
                LocalMapController.prototype.params = params;
                return;
            case 'pageGMap':
                GoogleMapController.prototype.params = params;
                return;
            case 'pageCityArchives':
                CityController.prototype.params = params;
                return;
            case 'pageBookmarkList':
                BookmarkListController.prototype.pushParams(params);
                return;
            case 'pageBookmarkListSearch':
                BookmarkListController.prototype.searchparams = params;
                return;
            case 'pageItemSearchResult':
                SearchController.prototype.params = params;
                return;
            case 'pageBookmarkRegister':
                BookmarkRegisterController.prototype.params = params;
                return;
            case 'pageItemList':
                DetailProductController.prototype.params = params;
                return;
            case 'pageECsite':
                ECSiteController.prototype.params = params;
                return;
            default:
                return;
        }    
    },
    
    getBackTargetPageId: function(pageId) {
        PageChangeController.prototype.prePageId = pageId;
        switch(pageId) {
            case 'pageHome':
                PageChangeController.prototype.prePageId = '#';
                return PageChangeController.prototype.EXIT_APP;
            case 'pageBookmarkList':
                BookmarkListController.prototype.params.data.pop();
                return PageChangeController.prototype.PRE_PAGE; 
            case 'pageBookmarkListSearch':                
                BookmarkListController.prototype.params.newCreate = true;
                return PageChangeController.prototype.PRE_PAGE;
            case 'pageItemList':
                return (DetailProductController.prototype.params.doScanAction ? '#pageHome' : '');
            case 'pageBookmarkRegister':
                var isPageRegister = BookmarkRegisterController.prototype.backToRegisterPage();
                return isPageRegister ? PageChangeController.prototype.PRE_PAGE : PageChangeController.prototype.NO_CHANGE;
            case 'pageMonthArchives':
            case 'pageGenreArchives':
            case 'pageKeywordArchives':
            case 'pagePrefArchives':
            case 'pageSetting':
                return '#pageHome';
            default:
                return PageChangeController.prototype.PRE_PAGE;
        }
    },
    
    checkInternetConnection: function(pageId) {
        switch(pageId) {
            case 'pageGMap':
            case 'pageItemSearchResult':
            case 'pageItemList':
            case 'pageECsite':
                return Util.prototype.showInternetAlert();
            default:
                return true;
        }
    },
    
    /* List of pages ID */
    // PAGE_HOME: "pageHome",
    // PAGE_SCAN: "SCAN_ACTIVITY",
    // PAGE_SEARCH: "pageItemSearch",
    // PAGE_SEARCH_RESULTS: "pageItemSearchResult",
    // PAGE_GENRES: "pageGenreArchives",
    // PAGE_KEYWORDS: "pageKeywordArchives",
    // PAGE_MOUTHS: "pageMonthArchives",
    // PAGE_PREFECTURES: "pagePrefArchives",
    // PAGE_CITIES: "pageCityArchives",
    // PAGE_MAP_JAPAN: "pageJapanMap",
    // PAGE_MAP_LOCAL: "pageLocalMap",
    // PAGE_MAP_GOOGLE: "pageGMap",
    // PAGE_BOOKMARKSLIST: "pageBookmarkList",
    // PAGE_BOOKMARKS_SEARCH: "pageBookmarkListSearch",
    // PAGE_DETAIL: "pageItemList",
    // PAGE_ECLIST: "pageECsite",
    // PAGE_BOOKMARK: "pageBookmarkRegister",
    // PAGE_SETTING: "pageSetting",
};
